﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using Lamex.Library;
using Lamex.Base;
using Lamex.Common;

namespace Lamex.Main
{
    public partial class frmModify : frmShow
    {
        public frmModify(string UserName)
        {
            
            InitializeComponent();
            this.Load += new EventHandler(frmModify_Load);
            txtUser.Text = UserName;
            btnOk.Click += new EventHandler(btnOk_Click);
            txtUser.Focus();
        }
        private void frmModify_Load(object sender,EventArgs e)
        {
            if (false == BridgeFactory.InitializeBridge())//初始化桥接功能
            {
                return;
            }
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txtUser.Text))
            {
                XtraMessageBox.Show("用户名不能为空！");
                return;
            }
            
            if (string.IsNullOrEmpty(txtOldPassWord.Text))
            {
                XtraMessageBox.Show("旧密码不能为空！");
                return;
            }
            if (string.IsNullOrEmpty(txtFirstNewPassWord.Text))
            {
                XtraMessageBox.Show("新密码不能为空！");
                return;
            }
            if (string.IsNullOrEmpty(txtSecondNewPassWord.Text))
            {
                XtraMessageBox.Show("确认新密码不能为空！");
                return;
            }
            if(txtFirstNewPassWord.Text!=txtSecondNewPassWord.Text)
            {
                XtraMessageBox.Show("确认新密码和新密码不一样！");
                return;

            }
            if (txtFirstNewPassWord.Text.Length<6)
            {
                XtraMessageBox.Show("密码长度必须六位数以上！");
                return;

            }
            if(!System.Text.RegularExpressions.Regex.IsMatch(txtFirstNewPassWord.Text, @"^(?![0-9]+$)(?![a-zA-Z]+$)[0-9a-zA-Z]+$"))
            {
                XtraMessageBox.Show("密码必须为数字和字母组合！");
                return;
            }
            try
            {
                int m = BaseBuildSql.ExecuteScalar("select * from Sys_MyUser where Account='" + txtUser.Text+ "' and Password='" + txtOldPassWord.Text + "' ");
                if (m > 0)
                {
                    if (BaseBuildSql.ExecuteNonQuery("update Sys_MyUser set Password='" + txtFirstNewPassWord.Text + "',IsFirstLogin='N' where Account='" + txtUser.Text + "'") > 0)
                    {
                        XtraMessageBox.Show("保存成功！");
                        this.Close();

                    }
                    else
                    {
                        XtraMessageBox.Show("保存失败！");
                    }
                }
                else
                {
                    XtraMessageBox.Show("用户名或旧密码不一致！");
                    return;
                }
            }
            catch(Exception ex)
            {
                XtraMessageBox.Show(ex.Message);
            }
        }

       
    }
}